<?php
class Layout
{
    protected $_view_file = "";
    public $view;
    protected $title;
    protected $css;
    protected $js;
    protected $meta;
    public static function getParams(){
        $params = explode("/",$_SERVER["REQUEST_URI"]);
        $new_params = array();
        foreach($params as $key => $param){ 
            if(!empty($param)){
                //allow GET params
                $position = strpos($param,"?");
                if($position !== false)
                {
                    $param = substr($param,0,$position);
                }
                $new_params[]=$param;
            }
        }
        if(empty($new_params[0]) && empty($new_params[1]))
        {
            $new_params[0] = "index";
            $new_params[1] = "index";
        }
        else if(!empty($new_params[0]) && empty($new_params[1])){
            $new_params[1] = "index";
        }
        
        return $new_params;
    }
    
    public function render()
    {
        $uri = self::getParams();
        if(!empty($uri)){
            $controller_path = realpath(dirname(__FILE__) . "/../modules/" . $uri[0] . "/controller/");
            $view_path = realpath(dirname(__FILE__) . "/../modules/" . $uri[0] . "/view/");
            $class = null;
            if(file_exists($controller_path) && file_exists($view_path)){
                if(isset($uri[1])){
                    $c = realpath($controller_path . "/" . $uri[1] . ".php");
                    $v = realpath($view_path .  "/" . $uri[1] . ".phtml");
                    if(file_exists($c) && file_exists($v)){
                        $this->_view_file = $v;
                        include_once($c);
                        $class = new $uri[1];                        
                    }
                    else{
                        //Error Page
                        header("HTTP/1.1 404");
                        $this->_view_file = realpath(dirname(__FILE__) . "/../modules/error/view/index.phtml");
                        include_once(realpath(dirname(__FILE__) . "/../modules/error/controller/index.php"));
                        $class = new index(); 
                    }
                }
            }
            else{
                //Error Page
                header("HTTP/1.1 404");
                $this->_view_file = realpath(dirname(__FILE__) . "/../" . "/modules/error/view/index.phtml");
                include_once(realpath(dirname(__FILE__) . "/../modules/error/controller/index.php"));
                $class = new index();
            }
        }
        else{
            //Home Page
            $this->_view_file = realpath(dirname(__FILE__) . "/../" . "/modules/index/view/index.phtml");
            include_once(realpath(dirname(__FILE__) . "/../modules/index/controller/index.php"));
            $class = new index();
        }
        //run controller
        $class->run();
        
        if(isset($class->view))
            $this->view = $class->view;
        
        //set title
        if(isset($class->header->title))
            $this->title = $class->header->title;
        else
            $this->title = "";
        
        //run initialization
        $initialize = get_class_methods('Initialize');
        foreach($initialize as $init){
            $init_return = Initialize::$init($class);
            if($init == 'initHeaders'){
                $class->header->css = array_merge($init_return->css,$class->header->css);
                $class->header->js = array_merge($init_return->js,$class->header->js);
            }
        }
        
        //set stylesheets
        if(isset($class->header->css)){
            if(is_array($class->header->css) && !empty($class->header->css)){
                foreach($class->header->css as $sheets){
                    if(!empty($sheets))
                        $this->css .= '<link rel="stylesheet" type="text/css" href="' . $sheets . '" />'. "\n";
                }
            }
            else{
                $this->css .= '<link rel="stylesheet" type="text/css" href="' . $class->header->css . '" />'. "\n";
            }
        }
        else{
            $this->css = "";
        }
                
        //set javascripts
        if(isset($class->header->js)){
            if(is_array($class->header->js) && !empty($class->header->js)){
                foreach($class->header->js as $js){
                    if(!empty($js))
                        $this->js .= '<script type="text/javascript" src="' . $js . '"></script>' . "\n";
                }
            }
            else{
                $this->js .= '<script type="text/javascript" src="' . $class->header->js . '"></script>' . "\n";
            }
        }
        else{
            $this->js = "";
        }
        //set meta
        $this->meta .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">' . "\n";
        if(isset($class->header->meta)){
            if(is_array($class->header->js) && !empty($class->header->js)){
                foreach($class->header->meta as $meta){
                    if(isset($meta["name"]) && isset($meta["content"])){
                        $this->meta .= '<meta name="' .  $meta["name"] . '" content="' . $meta["content"] . '"/>' . "\n";    
                    }
                }
            }
        }
        
        //choose layout
        if(empty($class->_layout) || !isset($class->_layout)){
            include_once(realpath(dirname(__FILE__) . "/../" . "/layout/default.phtml"));
        }
        else{
            if($layout = realpath(dirname(__FILE__) . "/../" . "/layout/{$class->_layout}.phtml"))
                include_once($layout);
        }
    }
    
    private function content(){
        include_once($this->_view_file);
    }
    
    private function header(){
        $layout = realpath(dirname(__FILE__) . "/../layout/partials/header.phtml");
        if(file_exists($layout))
            include_once($layout);
    }
    
    private function footer(){
        $layout = realpath(dirname(__FILE__) . "/../layout/partials/footer.phtml");
        if(file_exists($layout))
            include_once($layout);
    }
    
    private function partial($file){
        $layout = realpath(dirname(__FILE__) . "/../layout/partials/{$file}.phtml");
        if(file_exists($layout))
            include_once($layout);
    }
    
    public function pageDefaults(){
        $this->header->title = "My Title"; //Default Title
        $this->header->css = array('');
        $this->header->js = array('');
        $this->_layout = null; //default layout
        $this->header->meta = array(array('name'=>'description','content'=>'This is my site'),
                                    array('name'=>'keywords','content'=>'site,site'));
    }
}